package com.moleq.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.moleq.db.DatabaseSchema;
import com.moleq.mgnetwork.Constant;
import com.moleq.model.NetWorkSetting;

public class NetWorkSettingService
{
	private SQLiteDatabase DBHelper;
	private Context mContext;
	private String errorString;
	private Object sharedContext;
	private Object shareDBPath;

	public NetWorkSettingService()
	{

	}

	public NetWorkSettingService(String shareDBPath)
	{

		Log.i("NetWorkSettingService TAG", "->>>>>> " + shareDBPath);
		this.DBHelper = SQLiteDatabase.openOrCreateDatabase(shareDBPath, null);

		// this.DBHelper =
		// SQLiteDatabase.openOrCreateDatabase(Constant.MGPOS_DB_PATH,null);
		// mContext = context;
	}

	public List<NetWorkSetting> FindAll()
	{
		List<NetWorkSetting> obj = new ArrayList<NetWorkSetting>();
		Cursor cursor = DBHelper.rawQuery("select * from t_network_setting",
				null);
		if (cursor != null)
		{

			while (cursor.moveToNext())
			{
				int ns_no = cursor.getInt(cursor
						.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_NO));
				String ns_name = cursor.getString(cursor
						.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_NAME));
				String ns_ip = cursor.getString(cursor
						.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_IP));
				int ns_station = cursor
						.getInt(cursor
								.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_STATION));
				String ns_type = cursor.getString(cursor
						.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_TYPE));

				obj.add(new NetWorkSetting(ns_no, ns_station, ns_name, ns_ip,
						ns_type));
				cursor.close();
			}
		}
		return obj;
	}

	public NetWorkSetting Find(int no)
	{
		NetWorkSetting obj = null;
		Cursor cursor = DBHelper.rawQuery(
				"select * from t_network_setting where ns_no  = " + no, null);
		if (cursor != null)
		{

			while (cursor.moveToNext())
			{
				int ns_no = cursor.getInt(cursor
						.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_NO));
				String ns_name = cursor.getString(cursor
						.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_NAME));
				String ns_ip = cursor.getString(cursor
						.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_IP));
				int ns_station = cursor
						.getInt(cursor
								.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_STATION));
				String ns_type = cursor.getString(cursor
						.getColumnIndex(DatabaseSchema.NetWorkSetting.NS_TYPE));

				obj = new NetWorkSetting(ns_no, ns_station, ns_name, ns_ip,
						ns_type);

				cursor.close();
			}
		}
		return obj;

	}

	public int getMaxNo()
	{
		int no = 0;
		Cursor cursor = DBHelper.rawQuery(
				"select Max(ns_no) from t_network_setting", null);
		if (cursor != null)
		{

			if (cursor.moveToNext())
			{
				no = cursor.getInt(0);
			}
			cursor.close();
		}
		return no;

	}

	public boolean checkStation(String station, String no)
	{
		boolean check = false;
		Cursor cursor = DBHelper.rawQuery(
				"select ns_station from t_network_setting where ns_station = "
						+ station + " and ns_no <> " + no, null);
		if (cursor != null)
		{

			if (cursor.moveToNext())
			{
				check = true;
				cursor.close();
			}
		}
		return check;

	}

	public boolean checkIp(String ip, String no)
	{
		boolean check = false;
		Cursor cursor = DBHelper.rawQuery(
				"select ns_ip from t_network_setting where ns_ip = '" + ip
						+ "'" + " and ns_no <> " + no, null);
		if (cursor != null)
		{

			if (cursor.moveToNext())
			{
				check = true;
			}
			cursor.close();
		}
		return check;

	}

	public void InsertOrUpdate(NetWorkSetting setting)
	{
		// TODO Auto-generated method stub

	}

}
